<?php

namespace Extension;

use Config\Cfg;

class Tools {
	/**
	 * 构造函数
	 */
	function __construct() {
	}
	/**
	 * 析构函数
	 */
	function __destruct() {
	}
	/**
	 * 获取当前时间
	 *
	 * @param string $format        	
	 * @return string
	 */
	public static function mb_GetCurDateTime($format = 'DATETIME') {
		$retData = '';
		switch ($format) {
			case 'DATETIME' :
				$retData = date ( 'Y-m-d H:i:s' );
				break;
			case 'DATE' :
				$retData = date ( 'Y-m-d' );
				break;
			case 'TIME' :
				$retData = date ( 'H:i:s' );
				break;
			default :
				$retData = date ( 'Y-m-d H:i:s' );
				break;
		}
		return $retData;
	}
	/**
	 * 计算时间差
	 *
	 * @param unknown $timeA        	
	 * @param unknown $timeB        	
	 * @return number
	 */
	public static function mb_GetTimeDifference($timeA, $timeB) {
		$date = floor ( (strtotime ( $timeA ) - strtotime ( $timeB )) / 86400 );
		$hour = floor ( (strtotime ( $timeA ) - strtotime ( $timeB )) % 86400 / 3600 );
		$minute = floor ( (strtotime ( $timeA ) - strtotime ( $timeB )) % 86400 % 3600 / 60 );
		$second = floor ( (strtotime ( $timeA ) - strtotime ( $timeB )) % 86400 % 60 );
		$timeDiff = $date * 24 * 60 * 60 + $hour * 60 * 60 + $minute * 60 + $second;
		return $timeDiff;
	}
	/**
	 * 随机
	 *
	 * @param number $len        	
	 * @param string $format        	
	 * @return string
	 */
	public static function mb_RandStr($len = 6, $format = 'CHARNUM') {
		switch ($format) {
			case 'ALL' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
				break;
			case 'CHAR' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~';
				break;
			case 'NUMBER' :
				$chars = '0123456789';
				break;
			case 'CHARNUM' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
				break;
			case 'LOWENG' :
				$chars = 'abcdefghijklmnopqrstuvwxyz';
				break;
			case 'UPENG' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
				break;
			case 'LOWNUM' :
				$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
				break;
			case 'UPNUM' :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
				break;
			case 'DICE' :
				$chars = '123456';
				break;
			default :
				$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
				break;
		}
		// mt_srand ( ( double ) microtime () * 1000000 * getmypid () );
		$password = "";
		while ( strlen ( $password ) < $len )
			$password .= substr ( $chars, (mt_rand () % strlen ( $chars )), 1 );
		return $password;
	}
	/**
	 * 输出日志
	 *
	 * @param string $word        	
	 * @param string $show        	
	 */
	public static function mb_LogResult($word = '', $show = TRUE) {
		if (Cfg::PROJECT_DEBUG == false)
			return;
		
		$logWord = '[' . date ( 'Y-m-d H:i:s' ) . '] ' . $word . "\r\n";
		$logPath = dirname ( __FILE__ ) . '\\Log\\';
		$logName = 'Log_' . date ( 'Y-m-d' ) . '.txt';
		$logFile = $logPath . $logName;
		
		if (! is_dir ( $logPath )) {
			mkdir ( $logPath );
		}
		$fp = null;
		if (! is_file ( $logFile )) {
			$fp = fopen ( $logFile, "w" );
		} else {
			$fp = fopen ( $logFile, "a" );
		}
		flock ( $fp, LOCK_EX );
		fwrite ( $fp, $logWord );
		flock ( $fp, LOCK_UN );
		fclose ( $fp );
		
		if ($show)
			echo $logWord;
	}
	public static function mb_DebugLog($word = '', $show = TRUE) {
			$logWord = '[' . date ( 'Y-m-d H:i:s' ) . '] ' . $word . "\r\n";
			$logPath = dirname ( __FILE__ ) . '\\Log\\';
			$logName = 'Log_' . date ( 'Y-m-d' ) . '.txt';
			$logFile = $logPath . $logName;
			
			if (! is_dir ( $logPath )) {
				mkdir ( $logPath );
			}
			$fp = null;
			if (! is_file ( $logFile )) {
				$fp = fopen ( $logFile, "w" );
			} else {
				$fp = fopen ( $logFile, "a" );
			}
			flock ( $fp, LOCK_EX );
			fwrite ( $fp, $logWord );
			flock ( $fp, LOCK_UN );
			fclose ( $fp );
			
			if ($show)
				echo $logWord;
	}
	/**
	 * 根据两点间的经纬度计算距离
	 *
	 * @param float $lat
	 *        	纬度值
	 * @param float $lng
	 *        	经度值
	 */
	public static function mb_GetDistance($lat1, $lng1, $lat2, $lng2) {
		$earthRadius = 6367000; // approximate radius of earth in meters
		$lat1 = ($lat1 * pi ()) / 180;
		$lng1 = ($lng1 * pi ()) / 180;
		$lat2 = ($lat2 * pi ()) / 180;
		$lng2 = ($lng2 * pi ()) / 180;
		$calcLongitude = $lng2 - $lng1;
		$calcLatitude = $lat2 - $lat1;
		$stepOne = pow ( sin ( $calcLatitude / 2 ), 2 ) + cos ( $lat1 ) * cos ( $lat2 ) * pow ( sin ( $calcLongitude / 2 ), 2 );
		$stepTwo = 2 * asin ( min ( 1, sqrt ( $stepOne ) ) );
		$calculatedDistance = $earthRadius * $stepTwo;
		return round ( $calculatedDistance );
	}
	/**
	 * 过滤掉emoji表情
	 *
	 * @param string $str        	
	 * @return mixed
	 */
	public static function mb_FilterEmoji($str) {
		$str = preg_replace_callback ( '/./u', function (array $match) {
			return strlen ( $match [0] ) >= 4 ? '' : $match [0];
		}, $str );
		
		return $str;
	}
}